package com.gm.blog.controller;

import com.gm.blog.enums.ResultEnum;
import com.gm.blog.enums.StateEnums;
import com.gm.blog.pojo.About;
import com.gm.blog.service.AboutService;
import com.gm.blog.utils.Page;
import com.gm.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/about")
public class AboutController {
    @Autowired
    private AboutService aboutService;

    /**
     * 添加通知
     * @param about
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<Object> save(@RequestBody About about){
        if(about!=null){
            aboutService.save(about);
            return new Result<>("通知添加成功");
        }else{
            return new Result<>(ResultEnum.PARAMS_ERROR);
        }
    }
    /**
     * 更具ID修改通知内容
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody About about){
        aboutService.update(about);
        return new Result<>("修改成功");
    }
    /**
     * 更具ID删除用户
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Result<Object> delete(@PathVariable Integer id){
        aboutService.delete(id);
        return new Result<>("数据删除成功");
    }
    /**
     * 更具ID查询用户
     */
    @RequestMapping(value = "/getById/{id}",method = RequestMethod.GET)
    public Result<Object> getById(@PathVariable Integer id){
        About about = aboutService.getById(id);
        return new Result<>(about);
    }
    /**
     * 阅读通知
     */
    @RequestMapping(value="/read",method = RequestMethod.GET)
    public Result<Object> read(){
        About read = aboutService.read();
        return new Result<>(read);
    }
    /**
     * 启用通知
     */
    @RequestMapping(value="/enable/{id}",method = RequestMethod.PUT)
    public Result<Object> enable(@PathVariable Integer id){
        aboutService.ebabled(id);
        return new Result<>(StateEnums.ENABLED.getCode(),"启用成功");
    }
    /**
     * 启用通知
     */
    @RequestMapping(value = "/disable/{id}",method = RequestMethod.PUT)
    public Result<Object> disable(@PathVariable Integer id){
        aboutService.disable(id);
        return new Result<>(StateEnums.NOT_ENABLE.getCode(),"关闭成功");
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result<Object> getByPage(@RequestBody Page<About> page){
        page=aboutService.getByPage();
        return new Result<>(page);
    }
}
